<script>
import PubQuestionDialog from "@/components/comp/task_comm/PubQuestionDialogV2.vue";

export default {
  name: "PubTask",
  components: {PubQuestionDialog},
  created(){
    //初始化表格数据
    this.init_table_data();
  },
  data() {
      return {
            tableData:[],
            page: {
              pageNow: 1,
              pageSize: 7,
              tj1: '',
              tj3:'',
              total: 0
            },
            dialogVisible:false,
            acc_ids:'',
            acc_names:'',
            checked_rows:'',
            task:{
              uname:'',
              hastask:false,
              tx1_state:0, //1,进行中，2已完成
              tx2_state:0,
              tx4_state:0,
              tx5_state:0,
              tx6_state:0,
              tx7_state:0,
              tx8_state:0,
            }
      }
  },
  methods:{
    //初始化表格数据
    async init_table_data(){
      const res =  await this.$http.get("tasks/pub_task_list", {params:this.page} );
      const pageInfo = res.data.data;
      this.tableData = pageInfo.records;
      this.page.total = pageInfo.total;
    },

    del_row(row){
        if(row.testState==1){
            this.$confirm('此操作将删除用户当前未完成任务, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(async () =>  {
              //确认删除
               const res = await this.$http.get('tasks/del_acc_task?accId='+row.id);
              if(res.data.data){
                this.$message.success("恭喜，删除成功！");
                //刷新页面
                this.init_table_data();
              }else{
                this.$message.error("删除失败！！！");
              }
            }).catch(() => {
              this.$message.error("已取消删除！！！");
            });
        }else{
            this.$message.warning("当前用户无进行中的任务")
        }
    },



    sizeChange(val){
      this.page.pageSize = val;
      this.init_table_data();
    },

    pageChange(val){
      this.page.pageNow = val;
      this.init_table_data();
    },

    //性别显示格式化
    sexFormater(row){
      if(row.gender==0){
        return '男';
      }else {
        return '女';
      }
    },




    //修改、添加用户成功后，回调刷新
    option_ok_reload(){
        this.init_table_data();
    },

    //多选发生改变时
    handleSelectionChange(rows) {
      this.acc_names = '';
      this.acc_ids = "";
      this.checked_rows = '';
      this.checked_rows = rows;
      rows.forEach(row=>{
          this.acc_ids += row.id+",";
          this.acc_names += row.realName + ","
      })
    },



    //点击表格行时，选中复选框
    clickRow(row){
      this.$refs.accountTable.toggleRowSelection(row);
    },

    //批量发布任务
    pub_question(){
        //未选中用户时，禁止发布任务
        if(this.checked_rows.length){
          var b = false; //不存在未完成任务的用户
          this.checked_rows.forEach(row=>{
            if(row.testState==1){
              b = true;
            }
          })
          if(b){
            this.$message.error("用户存在未完成任务，重新发布前请清除！！！")
          }else{
            this.$refs.pub_question.showDialog(this.acc_ids,this.acc_names);
          }
        }else {
          this.$message.error("发布任务前，请选择用户！")
        }
    },

    show_info(row){

      //显示用户任务详情
       var task_no = row.backField2
      this.task.uname = row.realName
      if(task_no){
        this.task.hastask = true;
        this.$http.get("tasks/getTaskByTid?taskNo=" + task_no ).then(res=>{
          var task_list = res.data.data
          task_list.forEach(task=>{
            if(task.taskType==1){
              this.task.tx1_state = task.state;
            }else if(task.taskType==2){
              this.task.tx2_state = task.state;
            }else if(task.taskType==3){
              this.task.tx3_state = task.state;
            }else if(task.taskType==4){
              this.task.tx4_state = task.state;
            }else if(task.taskType==5){
              this.task.tx5_state = task.state;
            }else if(task.taskType==6){
              this.task.tx6_state = task.state;
            }else if(task.taskType==7){
              this.task.tx7_state = task.state;
            }
          })
          this.dialogVisible = true;
        })
      }else{
        this.task.hastask = false;
        this.dialogVisible = true;
      }
    }

  }
}
</script>

<template>
  <div>
<!-------------------------------------------导航组件------------------------------------------------------------->
    <el-row>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>任务发布</el-breadcrumb-item>
      </el-breadcrumb>
    </el-row>
<!-------------------------------------------搜索组件------------------------------------------------------------->
    <el-card class="box-card" style="margin-top: 20px">
      <el-row :gutter="20">
        <el-col :span="5" >
          <el-input @keyup.native.enter="init_table_data" v-model="page.tj1"  placeholder="请输入姓名、手机" >
            <i class="el-icon-search" slot="suffix"  style="line-height: 40px" @click="init_table_data"></i>
          </el-input>
        </el-col>

        <el-col :span="3" >
          <el-select v-model="page.tj3" placeholder="任务状态" clearable @change="init_table_data" >
              <el-option  label="未测试" value="0"></el-option>
              <el-option  label="进行中" value="1"></el-option>
              <el-option  label="已完成" value="2"></el-option>
          </el-select>
        </el-col>

        <el-col :span="2" :offset=13>
          <el-button @click="pub_question" type="primary" >发布任务</el-button>
        </el-col>

      </el-row>
<!-------------------------------------------表格组件------------------------------------------------------------->
      <el-row style="margin-top: 20px">

        <el-table
            :data="tableData"
            ref="accountTable"
            border
            style="width: 100%"
            @row-click="clickRow"
            @selection-change="handleSelectionChange"
        >

          <el-table-column
              type="selection"
              width="55">
          </el-table-column>

          <el-table-column
              prop="realName"
              label="姓名"
              align="center"
              width="120">
          </el-table-column>

          <el-table-column
              prop="gender"
              label="性别"
              align="center"
              width="120"
              :formatter="sexFormater"
          >
          </el-table-column>

          <el-table-column
              prop="age"
              width="120"
              align="center"
              label="年龄">
            <template slot-scope="scope">
              {{scope.row.age}}岁
            </template>
          </el-table-column>

          <el-table-column
              prop="phone"
              label="电话"
              align="center"
              width="140">
          </el-table-column>

          <el-table-column
              prop="backField2"
              label="任务编号"
              align="center">
          </el-table-column>

          <el-table-column
              prop="backField3"
              label="任务名称"
              align="center">
          </el-table-column>

          <el-table-column
              prop="testState"
              label="任务状态"
              align="center"
          >
              <template slot-scope="scope">
                <el-tag v-if="scope.row.testState=='0'" effect="dark" type="warning" disable-transitions>未测试</el-tag>
                <el-tag v-if="scope.row.testState=='1'" effect="dark" type="primary" disable-transitions>进行中</el-tag>
                <el-tag v-if="scope.row.testState=='2'" effect="dark" type="success" disable-transitions>已完成</el-tag>
              </template>
          </el-table-column>


          <el-table-column
              fixed="right"
              label="操作"
              align="center"
              width="140">
            <template slot-scope="scope">
<!--              <el-button @click="show_info(scope.row)" type="primary" size="small" icon="iconfont icon-chakanxiangqing2" circle></el-button>-->
              <el-button @click="del_row(scope.row)" type="danger" icon="iconfont icon-qingkong" size="small" circle></el-button>
            </template>
          </el-table-column>
        </el-table>

<!-------------------------------------------分页组件------------------------------------------------------------->
        <el-pagination
            @current-change="pageChange"
            @size-change="sizeChange"
            :current-page="page.pageNow"
            :page-sizes="[7, 15, 25, 50]"
            :page-size="page.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            style="margin-top: 20px;float:right"
            :total="page.total">
        </el-pagination>
      </el-row>
    </el-card>

<!-------------------------------------------用户列表子组件------------------------------------------------------------->
    <PubQuestionDialog ref="pub_question" @pubed="option_ok_reload"></PubQuestionDialog>
<!--    -->
    <el-dialog
        title=""

        :visible.sync="dialogVisible"
        width="30%">
        <div v-if="task.hastask">
          用户{{task.uname}}当前无任务
        </div>
        <div v-else>
          <h2 style="margin-bottom: 20px">{{task.uname}}进行中的任务：</h2>
          <el-tag v-if="task.tx1_state==1" effect="dark" style="margin-bottom:10px;font-size: 16px ">任务一</el-tag>&nbsp;&nbsp;
          <el-tag v-if="task.tx2_state==1" effect="dark" style="margin-bottom:10px;font-size: 16px ">任务二</el-tag>&nbsp;&nbsp;
          <el-tag v-if="task.tx3_state==1" effect="dark" style="margin-bottom:10px;font-size: 16px ">任务三</el-tag>&nbsp;&nbsp;
          <el-tag v-if="task.tx4_state==1" effect="dark" style="margin-bottom:10px;font-size: 16px ">任务四</el-tag>&nbsp;&nbsp;
          <el-tag v-if="task.tx5_state==1" effect="dark" style="margin-bottom:10px;font-size: 16px ">任务五</el-tag>&nbsp;&nbsp;
          <el-tag v-if="task.tx6_state==1" effect="dark" style="margin-bottom:10px;font-size: 16px ">任务六</el-tag>&nbsp;&nbsp;
          <el-tag v-if="task.tx7_state==1" effect="dark" style="margin-bottom:10px;font-size: 16px ">任务七</el-tag>&nbsp;&nbsp;

          <h2 style="margin:20px 0px;">{{task.uname}}已完成的任务：</h2>
          <el-tag v-if="task.tx1_state==2" effect="dark" type="success" style="margin-bottom:10px;font-size: 16px ">任务一</el-tag>&nbsp;&nbsp;
          <el-tag v-if="task.tx2_state==2" effect="dark" type="success" style="margin-bottom:10px;font-size: 16px ">任务二</el-tag>&nbsp;&nbsp;
          <el-tag v-if="task.tx3_state==2" effect="dark" type="success" style="margin-bottom:10px;font-size: 16px ">任务三</el-tag>&nbsp;&nbsp;
          <el-tag v-if="task.tx4_state==2" effect="dark" type="success" style="margin-bottom:10px;font-size: 16px ">任务四</el-tag>&nbsp;&nbsp;
          <el-tag v-if="task.tx5_state==2" effect="dark" type="success" style="margin-bottom:10px;font-size: 16px ">任务五</el-tag>&nbsp;&nbsp;
          <el-tag v-if="task.tx6_state==2" effect="dark" type="success" style="margin-bottom:10px;font-size: 16px ">任务六</el-tag>&nbsp;&nbsp;
          <el-tag v-if="task.tx7_state==2" effect="dark" type="success" style="margin-bottom:10px;font-size: 16px ">任务七</el-tag>&nbsp;&nbsp;
        </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>